package com.SMP.controller.class_room;

import com.SMP.model.Attendance;
import com.SMP.model.Lessons;
import com.SMP.model.Msg;
import com.SMP.service.class_room.AttendanceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AttendanceController {
    @Autowired
    AttendanceService attendanceService;

    /**
     * 增加考勤记录
     * @author WYHY
     *
     * */
    @RequestMapping("addAttendance")
    @ResponseBody
    public Msg addAttendance(Attendance attendance){
//        int result=attendanceService.addAttendance(attendance);
//        if (result==0){
//            return Msg.fail();
//        }else {
            return Msg.success();
//        }
    }

    /**
     * 修改考勤记录
     * @author WYHY
     *
     * */
    @RequestMapping("updAttendance")
    @ResponseBody
    public Msg updAttendance(Attendance attendance){
        int result=attendanceService.updAttendance(attendance);
        if (result==0){
            return Msg.fail();
        }else {
            return Msg.success();
        }
    }

    /**
     * 条件查询考勤记录
     * @author WYHY
     *
     * */
    @RequestMapping("selAttendance")
    @ResponseBody
    public Msg selAttendance(Attendance attendance){
        List<Attendance> result=attendanceService.selAttendance(attendance);
        if (result==null){
            return Msg.fail();
        }else {
            return Msg.success().add("attendance",result);
        }
    }
    /**
     * @author TSY
    // * @param 查询所有教师出勤记录
     */
    @RequestMapping("selTeaAttAll")
    @ResponseBody
    public Msg findTeaAttAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 5);
        List<Attendance> attendance = attendanceService.getTeaAll();
        PageInfo page = new PageInfo(attendance, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 按id查询单个教师出勤记录
     */
    @RequestMapping("selTeaAtt")
    @ResponseBody
        public Msg findTeaAtt(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "id",defaultValue = "1") String id){
        PageHelper.startPage(pn, 5);
            List<Attendance> attendance1 =attendanceService.getTeaById(id);
            PageInfo page = new PageInfo(attendance1, 5);
            return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 按名字查询单个教师出勤记录
     */
    @RequestMapping("selTeaAt")
    @ResponseBody
    public Msg findTeaAt(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "name") String name){
        PageHelper.startPage(pn, 5);
            List<Attendance> attendance1 =attendanceService.getTeaByName(name);
            PageInfo page = new PageInfo(attendance1, 5);
            return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 查询单个学生出勤记录
     */
    @RequestMapping("selStuAtt")
    @ResponseBody
    public Msg findStuAtt(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "name") String name){
        PageHelper.startPage(pn, 5);
        System.out.println(pn+name);
        List<Attendance> attendance1 =attendanceService.getStuByName(name);
        PageInfo page = new PageInfo(attendance1, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 查询单个学生出勤记录
     */
    @RequestMapping("selStuAt")
    @ResponseBody
    public Msg findStuAt(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "id") String id){
        PageHelper.startPage(pn, 5);
        List<Attendance> attendance1 =attendanceService.getStuById(id);
        PageInfo page = new PageInfo(attendance1, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 查询所有学生出勤记录
     */
    @RequestMapping("selStuAttAll")
    @ResponseBody

    public Msg findStuAttAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 5);
        List<Attendance> attendance = attendanceService.getStuAll();
        PageInfo page = new PageInfo(attendance, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 按照学科查询所有学生出勤记录
     */
    @RequestMapping("selStuAttAllByLes")
    @ResponseBody

    public Msg findStuAttByLesAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "lessonsId", defaultValue = "1") String lessonsId){
        PageHelper.startPage(pn, 5);
        List<Attendance> attendance = attendanceService.getLessStuAll(lessonsId);
        PageInfo page = new PageInfo(attendance, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 按照年级查询所有学生出勤记录
     */
    @RequestMapping("selByGradeAndClasss")
    @ResponseBody

    public Msg findByGrade(@RequestParam(value = "pn", defaultValue = "1") Integer pn,@RequestParam(value = "grade") String grade,@RequestParam(value = "classs") String classs){
        PageHelper.startPage(pn, 5);
        List<Attendance> attendance = attendanceService.getByClasssAndGrade(grade,classs);
        PageInfo page = new PageInfo(attendance, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 查询年级
     */
    @RequestMapping("selGrade")
    @ResponseBody

    public Msg findGrade(){
        List<Attendance> attendance = attendanceService.getGrade();
        return Msg.success().add("attendance", attendance);
    }
    /**
     * @author TSY
    // * @param 根据年级查班级
     */
    @RequestMapping("selClasss")
    @ResponseBody

    public Msg findClasss(String grade){
        List<Attendance> attendance = attendanceService.getClasss(grade);

        return Msg.success().add("attendance", attendance);
    }
}
